'''
Created on 31/10/2009

@author: kuryaki
'''
from bluetooth import *
import googleSearchClient as gsc

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

uuid = "00000000-1000-0001-0008-000000805f9b"

advertise_service( server_sock, "ServidorEjemplo",
                   service_id = uuid,
                   service_classes = [ uuid, SERIAL_PORT_CLASS ],
                   profiles = [ SERIAL_PORT_PROFILE ], 
#                   protocols = [ OBEX_UUID ] 
                    )
                   
print "Esperando conexion en el canal  RFCOMM  %d" % port

client_sock, client_info = server_sock.accept()
print "Conexion aceptada desde:  ", client_info

try:
    while True:
        data = client_sock.recv(1024)
        if len(data) == 0: break
        resultado = gsc.main(data[2:])
        tam = '0000' + hex(len(resultado))[2:]
        size = tam[len(tam)-4:]
        prefix = ('\\x'+size[:2]+'\\x'+size[2:]).decode('string_escape')
        client_sock.send(prefix+resultado)
except IOError:
    pass

print "Desconectado"

print "Todo Terminado"
            
client_sock.close()
server_sock.close()
